import { Module } from '@nestjs/common';
import { FactoryController } from './factory.controller';
// import { FactoryService } from './factory.service';
import { MyOptionsProvider } from './MyOptionsProvider';
import { DatabaseConnection } from './DatabaseConnection';
import { SomeOptionalProvider } from './SomeOptionalProvider';

const connectionProvider = {
  provide: 'CONNECTION',
  useFactory: (optionsProvider: MyOptionsProvider, optionalProvider?: any) => {
    const options = optionsProvider.get();
    optionalProvider?.modifyOptions(options);
    return new DatabaseConnection(options);
  },
  inject: [
    MyOptionsProvider,
    { token: 'SomeOptionalProvider', optional: true },
  ],
};

@Module({
  controllers: [FactoryController],
  providers: [
    connectionProvider,
    MyOptionsProvider,
    {
      provide: 'SomeOptionalProvider',
      // useValue: 'anything', // 可以是字符串
      useValue: new SomeOptionalProvider(),
    },
  ],
  exports: [connectionProvider],
})
export class FactoryModule {}
